package io.wit.modules.sys.service.impl;

import io.wit.common.annotation.DataFilter;
import io.wit.modules.sys.dao.EmployeeRoleDao;
import io.wit.modules.sys.entity.SysRoleEntity;
import io.wit.modules.sys.entity.SysUserEntity;
import io.wit.modules.sys.service.EmployeeRoleMenuService;
import io.wit.modules.sys.service.EmployeeRoleService;
import io.wit.modules.sys.shiro.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工角色类型
*/
@Service("employeeRoleService")
public class EmployeeRoleServiceImpl implements EmployeeRoleService{

    @Autowired
	private EmployeeRoleDao employeeRoleDao;

    @Autowired
	private EmployeeRoleMenuService employeeRoleMenuService;

	@Override
	public SysRoleEntity queryObject(Long roleId) {
		return employeeRoleDao.queryObject(roleId);
	}

	@Override
	@DataFilter(tableAlias = "r", user = false)
	public List<SysRoleEntity> queryList(Map<String, Object> map) {
		return employeeRoleDao.queryList(map);
	}

	@Override
	@DataFilter(tableAlias = "r", user = false)
	public int queryTotal(Map<String, Object> map) {
		return employeeRoleDao.queryTotal(map);
	}

	@Override
	@Transactional
	public int save(final SysRoleEntity role){
        final SysUserEntity user = ShiroUtils.getUserEntity();
        role.setSubjectId(user.getSubjectId());
        final int rows = employeeRoleDao.save(role);
        //保存角色与菜单关系
        employeeRoleMenuService.saveOrUpdate(role.getRoleId(),role.getMenuIdList());
        return rows;
    }

	@Override
	@Transactional
	public void update(final SysRoleEntity role) {
        //更新角色与菜单关系
        employeeRoleMenuService.saveOrUpdate(role.getRoleId(),role.getMenuIdList());
        employeeRoleDao.update(role);
    }

	@Override
	@Transactional
	public void deleteBatch(Long[] roleIds) {
		employeeRoleDao.deleteBatch(roleIds);
	}

	//加载当前账号所拥有的权限菜单
    @Override
    public List<HashMap> queryUserMenu(final Map<String,Object> map){
        final SysUserEntity user = ShiroUtils.getUserEntity();
        final Long userId = user.getUserId();
        return employeeRoleDao.queryUserMenu(userId);
    }
}